package com.wgl.va.command.component;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Variant;

public class BaseEXTool extends BaseActiveXTool {
	private String softId = null;
	private ActiveXComponent activeDm = null;
	private static Logger logger = Logger.getLogger(BaseEXTool.class);

	public BaseEXTool(String softId) {
		this.softId = softId;
	}

	public void setSoftId(String softId) {
		this.softId = softId;
	}

	public ActiveXComponent getActiveXComponent() {
		try {
			if (activeDm == null) {
				activeDm = BaseActiveXTool.getActiveXComponent(this.softId);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return activeDm;
	}

	public int invokeMethodInt(List varList, String methodName) {
		return this.invokeMethod(varList, methodName).getInt();
	}

	public String invokeMethodString(List varList, String methodName) {
		return this.invokeMethod(varList, methodName).getString();
	}

	public String invokeMethodString(String methodName) {
		return this.invokeMethod(null, methodName).getString();
	}

	public int invokeMethodInt(String methodName) {
		return this.invokeMethod(null, methodName).getInt();
	}

	public Variant invokeMethod(List varList, String methodName) {
		Variant returnVar = null;
		try {
			logger.info("执行方法:" + methodName);
			if (varList != null && varList.size() > 0) {
				int size = varList.size();
				Variant[] var = new Variant[size];
				for (int i = 0; i < size; i++) {
					var[i] = new Variant(varList.get(i));
				}
				returnVar = this.getActiveXComponent().invoke(methodName, var);
			} else {
				returnVar = this.getActiveXComponent().invoke(methodName);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnVar;
	}

	public int invokeMethodInt(String methodName, Object... args) {
		return this.invokeMethodInt(this.getObjVarList(args), methodName);
	}

	public String invokeMethodInt(String methodName, int... args) {
		return this.invokeMethodString(this.getIntVarList(args), methodName);
	}

	public int invokeMethodIntWithInt(String methodName, int... args) {
		return this.invokeMethodInt(this.getIntVarList(args), methodName);
	}

	public String invokeMethodString(String methodName, Object... args) {
		return this.invokeMethodString(this.getObjVarList(args), methodName);
	}

	public int invokeMethodString(String methodName, String... args) {
		return this.invokeMethodInt(this.getStringVarList(args), methodName);
	}

	/**
	 * 必须输入全部参数，不能中间断开否则将会错位
	 * 
	 * @param args
	 * @return
	 */
	public List getIntVarList(int... args) {
		List varList = new ArrayList();
		for (int i = 0; i < args.length; i++) {
			varList.add(args[i]);
		}
		return varList;
	}

	/**
	 * 必须输入全部参数，不能中间断开否则将会错位
	 * 
	 * @param args
	 * @return
	 */
	public List getStringVarList(String... args) {
		List varList = new ArrayList();
		for (int i = 0; i < args.length; i++) {
			varList.add(args[i]);
		}
		return varList;
	}

	public List getStringVarList(char... args) {
		List varList = new ArrayList();
		for (int i = 0; i < args.length; i++) {
			varList.add(args[i]);
		}
		return varList;
	}

	/**
	 * 必须输入全部参数，不能中间断开否则将会错位
	 * 
	 * @param args
	 * @return
	 */
	public List getObjVarList(Object... args) {
		List varList = new ArrayList();
		for (int i = 0; i < args.length; i++) {
			varList.add(args[i]);
		}
		return varList;
	}

}
